'use strict';
const db = uniCloud.database()
const coursesCollection = db.collection('courses')

exports.main = async (event, context) => {
	const { courseId } = event
	
	try {
		if (!courseId) {
			return {
				code: -1,
				msg: '课程ID不能为空'
			}
		}
		
		// 检查课程是否存在
		const course = await coursesCollection.doc(courseId).get()
		if (!course.data || course.data.length === 0) {
			return {
				code: -1,
				msg: '课程不存在'
			}
		}
		
		// 删除课程
		await coursesCollection.doc(courseId).remove()
		
		return {
			code: 0,
			msg: '删除成功'
		}
	} catch (e) {
		console.error('删除课程失败', e)
		return {
			code: -1,
			msg: '删除失败',
			error: e
		}
	}
} 